package alo.p5.work;

import java.util.Stack;

//使用两个栈实现队列
public class QueueWithTwoStacks {
    Stack stack1;
    Stack stack2;
    public QueueWithTwoStacks(){
        stack1=new Stack();
        stack2=new Stack();
    }
    public boolean isEmpty(){
        //判断队列是否为空的下一个操作一般是出队列
        //所以这里提前把栈1元素分到栈2
        //便于出队列操作
        if (stack2.isEmpty()){
            while (!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.isEmpty();
    }

    public void enQueue(int data){
        stack1.push(data);
    }
    public Object deQueue(){
        if (!stack2.isEmpty()){
            return stack2.pop();
        }else {
            while (!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
}
